package client;

import java.awt.RenderingHints.Key;
import java.io.*;
import java.math.BigInteger;

import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.UnrecoverableEntryException;
import java.security.cert.CertificateException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;


public class EstraiChiaviCertClient {

	public EstraiChiaviCertClient(){
	
	}
	
	/*public static void main(String[] args) {
		EstraiChiaviCertClient ec = new EstraiChiaviCertClient();
		
		//PublicKey pub= ec.getMyPubKey();
		//System.out.println(pub);
		//PrivateKey priv = ec.getMyPrivKey();
		//System.out.println(priv);
		ec.ScriviChiaveSuFile();
		//PublicKey pub = ec.leggiChiaveDaFile();
		System.out.println(pub);
	}*/

	
	
	
	
	
	@SuppressWarnings("finally")
	public PublicKey getMyPubKey(){
		char[] password = "client".toCharArray();
		String alias = "clientKey";
		PublicKey clientPubKey=null;
		try {
			
			FileInputStream fis = new FileInputStream("./certificati/clientKeystore.jks");
			KeyStore keystore = KeyStore.getInstance("JKS");
			keystore.load(fis, password);
			
			java.security.cert.Certificate cer = keystore.getCertificate(alias);
			
			//ricavo chiave pubblica
			byte[] publicKeyBytes;
			publicKeyBytes =cer.getPublicKey().getEncoded();
			X509EncodedKeySpec ks = new X509EncodedKeySpec(publicKeyBytes);
			KeyFactory kf = KeyFactory.getInstance("RSA");
			clientPubKey = kf.generatePublic(ks);
			
		} catch (FileNotFoundException e) {
			System.out.println("FileNotFound");
		} catch (KeyStoreException e) {
			System.out.println("KeyStoreException");
		} catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlg");
		} catch (CertificateException e) {
			System.out.println("Certificate");
		} catch (IOException e) {
			System.out.println("IO");
		} 
	finally{
		return clientPubKey;
	}
	}
	
	@SuppressWarnings("finally")
	public PrivateKey getMyPrivKey(){
		char[] password = "client".toCharArray();
		String alias = "clientKey";
		PrivateKey clientPrivKey=null;
		try {
			
			FileInputStream fis = new FileInputStream("./certificati/clientKeystore.jks");
			KeyStore keystore = KeyStore.getInstance("JKS");
			keystore.load(fis, password);
			
			java.security.cert.Certificate cer = keystore.getCertificate(alias);
			//ricavo chiave privata
			KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) keystore.getEntry(alias, new KeyStore.PasswordProtection(password));
			byte[] privateKeyBytes = pkEntry.getPrivateKey().getEncoded();
			
			PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(privateKeyBytes);
			KeyFactory kf = KeyFactory.getInstance("RSA");
			clientPrivKey = kf.generatePrivate(ks);
			
		} catch (FileNotFoundException e) {
			System.out.println("FileNotFound");
		} catch (KeyStoreException e) {
			System.out.println("KeyStoreException");
		} catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlg");
		} catch (CertificateException e) {
			System.out.println("Certificate");
		} catch (IOException e) {
			System.out.println("IO");
		} 
	finally{
		return clientPrivKey;
	}
	}
	
	
	public void ScriviChiaveSuFile(String nome_client){
		String FileName = nome_client+"PublicKey.key";
		System.out.println(FileName);
		char[] password = "client".toCharArray();
		String alias = "clientKey";
		RSAPublicKey key = null;
		try {
			
			FileInputStream fis = new FileInputStream("./certificati/clientKeystore.jks");
			KeyStore keystore = KeyStore.getInstance("JKS");
			keystore.load(fis, password);
			
			java.security.cert.Certificate cer = keystore.getCertificate(alias);
			byte[] publicKeyBytes;
			publicKeyBytes =cer.getPublicKey().getEncoded();
			X509EncodedKeySpec ks = new X509EncodedKeySpec(publicKeyBytes);
			KeyFactory kf = KeyFactory.getInstance("RSA");
			key = (RSAPublicKey)kf.generatePublic(ks);
			
			
			
			FileOutputStream f = new FileOutputStream (FileName);
			ObjectOutputStream o = new ObjectOutputStream(f);
			o.writeObject(key.getModulus());
			o.writeObject(key.getPublicExponent());
			o.close();
			
		} catch (FileNotFoundException e) {
			System.out.println("FileNotFound");
		} catch (KeyStoreException e) {
			System.out.println("KeyStoreException");
		} catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlg");
		} catch (CertificateException e) {
			System.out.println("Certificate");
		} catch (IOException e) {
			System.out.println("IO");
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	
	}
	
	//credo che questa non mi serva
	public PublicKey leggiChiaveDaFile(){
		String FileName = "clientPublicKey.key";
		
		
		PublicKey key = null;
		
		try {
			
			FileInputStream fis = new FileInputStream(FileName);
			ObjectInputStream os = new ObjectInputStream(fis);
			BigInteger mod = (BigInteger) os.readObject();
			BigInteger exp = (BigInteger) os.readObject();
			RSAPublicKeySpec keySpec = new RSAPublicKeySpec(mod, exp);
			KeyFactory fact = KeyFactory.getInstance("RSA");
			key=fact.generatePublic(keySpec);
			os.close();
			
			
			
		} catch (FileNotFoundException e) {
			System.out.println("FileNotFound");
		}  catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlg");
		}  catch (IOException e) {
			System.out.println("IO");
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		finally {
			return key;
		}
	}
	
	
	
}
